﻿Imports BUS
Imports DTO
Public Class FormThanhToanHD
    Public _flag As Integer = 0
    Public _maHD As Integer
    Dim _ctBUS As New ChiTietBUS()
    Dim _tdBUS As New ThucDonBUS()
    Dim _hdBUS As New HoaDonBUS()
    Private Sub FormThanhToanHD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadGV()
    End Sub

    Public Sub LoadGV()

        Dim dt As New DataTable()
        dt = _ctBUS.LayCTHoaDonBUS(_maHD)        
        dtGVCTHoaDon.DataSource = dt

        Dim _tongTien As Double = 0
        For Each row As DataRow In dt.Rows
            _tongTien += Double.Parse(row("TongTien").ToString())
        Next
        lbTongTien.Text = "Tổng tiền hoá đơn là: " & _tongTien

    End Sub

    Private Sub btnThanhToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThanhToan.Click
        _flag = 1
        Me.Close()
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Me.Close()
    End Sub


    'Public Sub LoadlvCTHD()
    '    lvCTHD.Items.Clear()
    '    Dim _listCTHD As New List(Of ChiTietDTO)
    '    _listCTHD = _ctBUS.LoadDanhSachChiTietBUS(_maHD)

    '    Dim _tongTien As Double = 0

    '    Dim stt = 1
    '    For Each _chiTiet As ChiTietDTO In _listCTHD


    '        Dim _item As New ListViewItem(_chiTiet.MaCT.ToString())
    '        Dim _thucDonDTO As ThucDonDTO = _tdBUS.LayThongTinThucDonBUS(_chiTiet.MaMenu)
    '        _item.SubItems.Add(stt)
    '        _item.SubItems.Add(_chiTiet.MaHD.ToString())
    '        _item.SubItems.Add(_thucDonDTO.Ten)

    '        If (_chiTiet.MaMenu = 1 Or _chiTiet.MaMenu = 2 Or _chiTiet.MaMenu = 3) Then
    '            Dim _hdDTO As HoaDonDTO = _hdBUS.LayThongTinHDBUS(_chiTiet.MaHD)
    '            Dim _thoiGianSuDung As Integer = DateDiff(DateInterval.Hour, _hdDTO.DTThoiGianLap, DateTime.Now)

    '            If (_thoiGianSuDung = 0) Then
    '                _thoiGianSuDung = 1
    '            End If

    '            _item.SubItems.Add(_thoiGianSuDung.ToString())
    '            _item.SubItems.Add(_chiTiet.DonGia.ToString())
    '            _item.SubItems.Add((_thoiGianSuDung * _chiTiet.DonGia).ToString())
    '            _tongTien += _thoiGianSuDung * _chiTiet.DonGia
    '        Else
    '            _item.SubItems.Add(_chiTiet.SoLuong.ToString())
    '            _item.SubItems.Add(_chiTiet.DonGia.ToString())
    '            _item.SubItems.Add(_chiTiet.TongTien.ToString())
    '            _tongTien += _chiTiet.TongTien
    '        End If

    '        lvCTHD.Items.Add(_item)
    '        stt += 1
    '    Next

    '    lbTongTien.Text &= _tongTien & "vnđ"
    'End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click

        Dim _hoanTat As Integer = 0

        Dim dt As New DataTable()
        dt = CType(dtGVCTHoaDon.DataSource, DataTable)
        For Each row As DataRow In dt.Rows
            If (row.RowState = DataRowState.Modified) Then
                _hoanTat = 1

                Dim _maCT As Integer = Integer.Parse(row("MaCT").ToString())
                Dim _maMenu As Integer = Integer.Parse(row("MaMenu").ToString())
                Dim _soLuong As Integer = Integer.Parse(row("Soluong", DataRowVersion.Current).ToString())

                Dim _soLuongTon As Integer = Integer.Parse(row("TinhTrang").ToString())


                If (_soLuong > _soLuongTon AndAlso _maMenu <> 1 AndAlso _maMenu <> 2 AndAlso _maMenu <> 3) Then
                    MessageBox.Show("Số lượng tồn không thẻ nhỏ hơn số lượng")
                    LoadGV()
                    Return
                End If
                _ctBUS.SuaCTHDBUS(_maCT, _soLuong)
            End If
            If (row.RowState = DataRowState.Deleted) Then
                _hoanTat = 1
                Dim _maCT As Integer = Integer.Parse(row("MaCT", DataRowVersion.Original).ToString())
                _ctBUS.XoaCTHDBUS(_maCT)
            End If
        Next
        If (_hoanTat = 1) Then
            MessageBox.Show("Thao tác sửa/ xoá thành công", "Thông báo!")
            LoadGV()
        End If
    End Sub
End Class